package export.magicWeapon
{
	import base.BaseAddEffect;
	import base.BaseBitmapDataClip;
	import base.BaseBitmapDataPool;
	import base.BaseBullet;
	import base.BaseHero;
	import base.BaseMagicWeapon;
	import base.BaseMonster;
	
	import com.dusk.util.McUtil;
	import com.game.data.vo.EquipmentVO;
	import com.greensock.TweenMax;
	
	import export.bullet.EnemyMoveBullet;
	import export.bullet.SpecialEffectBullet;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	public class MagicPearl extends BaseMagicWeapon
	{
		
		public function MagicPearl(param1:BaseHero)
		{
			this.protectedObj = {"attackTime": 0};
			super(param1);
		}
		
		private var bullet:EnemyMoveBullet;
		private var effect:SpecialEffectBullet;
		private var target:BaseMonster;
		private var protectedObj:Object;
		private var attacktimes:int;
		private var times:int;
		private var lastx:Number;
		private var lasty:Number;
		private var xx:Number;
		private var yy:Number;
		private var dir:int;
		private var hit1:Boolean;
		private var hit2:Boolean;
		private var hit3:Boolean;
		private var attackbullet:SpecialEffectBullet;
		
		override public function step():void
		{
			super.step();
		}
		
		override protected function setPosition2():void
		{
			if (!this.isUsing())
			{
				super.setPosition2();
			}
		}
		
		override protected function initBBDC():void
		{
			var _loc1_:* = null;
			var _loc2_:Array = BaseBitmapDataPool.getBitmapDataArrayByName("MagicPearlBmd2");
			if (_loc2_)
			{
				_loc1_ = {
					"name"  : "body",
					"source": _loc2_
				};
				bbdc = new BaseBitmapDataClip([_loc1_], 2 * 60, 2 * 60, new Point(0, 0));
				bbdc.setOffsetXY(0, 0);
				bbdc.setFrameStopCount([[10], [9999]]);
				bbdc.setFrameCount([1, 1]);
				bbdc.setAddScriptWhenFrameOver(scriptFrameOverFunc);
				this.addChild(bbdc);
				return;
			}
			throw new Error("MagicPearl--BitmapData Error!");
		}
		
		override public function showSkill():void
		{
			var bb:SpecialEffectBullet;
			var equip:EquipmentVO;
			this.mp = 100 + Number(this.sourceRole.roleProperties.mp) * 0.02;
			bb = new SpecialEffectBullet("MagicPearlBegin");
			equip = this.sourceRole.getPlayer().getCurEquipByType("zbfb");
			this.attacktimes = 3 + equip.getLevel() / 3;
			if (equip.getFiveElementString().indexOf("木") != -1)
			{
				this.attacktimes += 2;
			}
			bb.x = this.x;
			bb.y = this.y;
			bb.setDisable();
			bb.setOwner(this.sourceRole);
			bb.setAction("idle");
			this.sourceRole.magicBulletArray.push(bb);
			gc.gameScene.addChild(bb);
			bb.setDirect(this.sourceRole.getBBDC().getDirect());
			bb.setFuncWhenDestroy(this.run);
			TweenMax.delayedCall(30, function (param1:MagicPearl):*
			{
				param1.setAction("idle");
			}, [this]);
		}
		
		public function hit1_1():void
		{
			var _loc1_:SpecialEffectBullet = null;
			var _loc2_:* = null;
			if (this.sourceRole)
			{
				_loc1_ = new SpecialEffectBullet("MagicPearlBullet1");
				_loc1_.x = this.xx;
				_loc1_.y = Number(this.yy) - 15;
				_loc1_.setOwner(this.sourceRole);
				_loc2_ = _loc1_.transform.matrix;
				_loc2_.a = this.transform.matrix.a;
				_loc1_.transform.matrix = _loc2_;
				_loc1_.setAction("fabao-pearl");
				_loc1_.setDirect(this.dir);
				gc.gameScene.addChild(_loc1_);
				this.sourceRole.magicBulletArray.push(_loc1_);
			}
		}
		
		public function hit1_2():void
		{
			var _loc1_:* = null;
			var _loc2_:* = null;
			if (this.sourceRole)
			{
				_loc1_ = new SpecialEffectBullet("MagicPearlBullet2");
				_loc1_.x = this.xx;
				_loc1_.y = this.yy;
				_loc1_.setRole(this.sourceRole);
				_loc2_ = _loc1_.transform.matrix;
				_loc2_.a = this.transform.matrix.a;
				_loc1_.transform.matrix = _loc2_;
				_loc1_.setAction("fabao-pearl");
				_loc1_.setDirect(this.dir);
				gc.gameScene.addChild(_loc1_);
				this.sourceRole.magicBulletArray.push(_loc1_);
			}
		}
		
		public function hit1_3():void
		{
			var _loc1_:* = null;
			var _loc2_:* = null;
			if (this.sourceRole)
			{
				_loc1_ = new SpecialEffectBullet("MagicPearlBullet3");
				_loc1_.x = this.xx;
				_loc1_.y = this.yy + 10;
				_loc1_.setRole(this.sourceRole);
				_loc2_ = _loc1_.transform.matrix;
				_loc2_.a = this.transform.matrix.a;
				_loc1_.transform.matrix = _loc2_;
				_loc1_.setAction("fabao-pearl");
				_loc1_.setDirect(this.dir);
				gc.gameScene.addChild(_loc1_);
				this.sourceRole.magicBulletArray.push(_loc1_);
			}
		}
		
		private function run(param1:BaseBullet):*
		{
			var _loc5_:Number = NaN;
			var _loc6_:int = 0;
			var _loc7_:BaseMonster = null;
			if (this.attackbullet)
			{
				this.attackbullet.removeEventListener(Event.ENTER_FRAME, this.func);
			}
			var _loc2_:int = 0;
			var _loc3_:BaseMonster = null;
			var _loc4_:Array = [];
			while (_loc2_ < gc.pWorld.getMonsterArray().length)
			{
				_loc3_ = gc.pWorld.getMonsterArray()[_loc2_] as BaseMonster;
				_loc4_.push({
					"index": _loc2_,
					"dis"  : AUtils.GetDisBetweenTwoObj(_loc3_, this.sourceRole)
				});
				_loc2_++;
			}
			_loc2_ = 0;
			if (_loc4_.length > 0)
			{
				_loc5_ = Number(_loc4_[0].dis);
				_loc6_ = int(_loc4_[0].index);
				while (_loc2_ < _loc4_.length - 1)
				{
					if (_loc5_ > _loc4_[_loc2_ + 1].dis)
					{
						_loc5_ = Number(_loc4_[_loc2_ + 1].dis);
						_loc6_ = int(_loc4_[_loc2_ + 1].index);
					}
					_loc2_++;
				}
				_loc7_ = gc.pWorld.getMonsterArray()[_loc6_];
			}
			++this.times;
			if (this.times > this.attacktimes)
			{
				this.times = 0;
				this.dowhendestroy();
				return;
			}
			if (!_loc7_)
			{
				this.dowhendestroy();
				return;
			}
			var _loc8_:SpecialEffectBullet = null;
			if (this.times > 1)
			{
				(_loc8_ = new SpecialEffectBullet("MagicPearlBack")).x = this.lastx;
				_loc8_.y = this.lasty;
			}
			else
			{
				(_loc8_ = new SpecialEffectBullet("MagicPearlRun")).x = this.x;
				_loc8_.y = this.y;
			}
			_loc8_.setDisable();
			_loc8_.setOwner(this.sourceRole);
			_loc8_.setAction("idle");
			this.sourceRole.magicBulletArray.push(_loc8_);
			gc.gameScene.addChild(_loc8_);
			if (this.times > 1)
			{
				if (this.lastx < _loc7_.x)
				{
					this.dir = 1;
					McUtil.flipHorizontal(_loc8_, -1);
					TweenMax.to(_loc8_, 0.5, {
						"x": _loc7_.x - 30,
						"y": _loc7_.y
					});
				}
				else
				{
					this.dir = 0;
					McUtil.flipHorizontal(_loc8_, 1);
					TweenMax.to(_loc8_, 0.5, {
						"x": _loc7_.x + 30,
						"y": _loc7_.y
					});
				}
			}
			else if (this.x < _loc7_.x)
			{
				this.dir = 1;
				McUtil.flipHorizontal(_loc8_, -1);
				TweenMax.to(_loc8_, 0.5, {
					"x": _loc7_.x - 30,
					"y": _loc7_.y
				});
			}
			else
			{
				this.dir = 0;
				McUtil.flipHorizontal(_loc8_, 1);
				TweenMax.to(_loc8_, 0.5, {
					"x": _loc7_.x + 30,
					"y": _loc7_.y
				});
			}
			this.lastx = _loc7_.x;
			this.lasty = _loc7_.y;
			this.xx = _loc7_.x;
			this.yy = _loc7_.y;
			_loc8_.setFuncWhenDestroy(this.attack);
		}
		
		private function attack(param1:BaseBullet):*
		{
			this.attackbullet = new SpecialEffectBullet("MagicPearlEffect");
			this.attackbullet.addEventListener(Event.ENTER_FRAME, this.func);
			this.attackbullet.x = this.xx;
			this.attackbullet.y = this.yy;
			this.attackbullet.setDisable();
			this.attackbullet.setOwner(this.sourceRole);
			this.attackbullet.setAction("idle");
			this.sourceRole.magicBulletArray.push(this.attackbullet);
			gc.gameScene.addChild(this.attackbullet);
			this.attackbullet.setDirect(this.dir);
			this.attackbullet.setFuncWhenDestroy(this.run);
		}
		
		private function dowhendestroy():*
		{
			var _loc1_:Number = Math.random();
			var _loc2_:EquipmentVO = this.sourceRole.getPlayer().getCurEquipByType("zbfb");
			var _loc3_:Number = _loc2_.getLevel();
			var _loc4_:Array = gc.pWorld.getMonsterArray();
			if (_loc2_.getFiveElementString().indexOf("木") != -1)
			{
				_loc3_ *= 1.5;
			}
			if (_loc1_ >= 0 && _loc1_ <= 0.33)
			{
				this.sourceRole.cureMp(sourceRole.getMpMax() * _loc3_ * 0.01);
			}
			else if (_loc1_ >= 0.34 && _loc1_ <= 0.66)
			{
				if (_loc4_.length == 0)
				{
					this.sourceRole.cureMp(sourceRole.getMpMax() * _loc3_ * 0.01);
					return;
				}
				for each (var mon:BaseMonster in _loc4_)
				{
					if (!mon.isInvincible() && !mon.isDead())
					{
						mon.addCurAddEffect([{
							"name": BaseAddEffect.STUN,
							"time": gc.frameClips * (_loc3_ / 8)
						}]);
					}
				}
			}
			else if (_loc1_ >= 0.67)
			{
				if (_loc4_.length == 0)
				{
					this.sourceRole.cureMp(sourceRole.getMpMax() * _loc3_ * 0.01);
					return;
				}
				for each (mon in _loc4_)
				{
					if (!mon.isInvincible() && !mon.isDead())
					{
						mon.addCurAddEffect([{
							"name" : BaseAddEffect.POISON,
							"time" : gc.frameClips * (_loc3_ / 4),
							"power": Number(this.sourceRole.getAtk()) * _loc3_ * 0.0413
						}, {
							"name": BaseAddEffect.POISON_TIMES,
							"time": gc.frameClips * (_loc3_ / 4)
						}]);
					}
				}
			}
		}
		
		private function func(param1:Event):*
		{
			if (this.attackbullet)
			{
				if (this.attackbullet.getClip().currentFrame == 3)
				{
					if (!this.hit1)
					{
						this.hit1_1();
						this.hit1 = true;
						this.hit2 = false;
					}
				}
				else if (this.attackbullet.getClip().currentFrame == 12)
				{
					if (!this.hit2)
					{
						this.hit1_2();
						this.hit2 = true;
						this.hit3 = false;
					}
				}
				else if (this.attackbullet.getClip().currentFrame == 28)
				{
					if (!this.hit3)
					{
						this.hit1_3();
						this.hit3 = true;
						this.hit1 = false;
					}
				}
			}
		}
	}
}
